package org.jboss.as.quickstarts.login.ws;

import org.jboss.as.quickstarts.login.User;
import org.jboss.as.quickstarts.login.UserManager;

import javax.inject.Inject;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Application;
import javax.ws.rs.core.Response;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * @author Gregory Rushton, gregory.rushton@gmail.com
 *         Date: 9/7/11
 *         Time: 4:54 PM
 */
@ApplicationPath( "/ws" )
@Path("/user")
public class UserDetails extends Application {

    @Inject
    private UserManager userManager;

    @Inject
    private transient Logger logger;

    @GET
    @Path("/{username}")
    public Response userService(@PathParam("username") String username) {
        User user = null;
        String errorMessage;
        if (username == null) {
            errorMessage = "Please Enter a username as part of the request.";
        } else {
            errorMessage = "Unable to find a user with the username: " + username;
        }
        try {
            if (username != null) {
                user = userManager.findUserByUsername(username);
            }
        } catch (Exception e) {
            logger.log(Level.ALL, e.getMessage());
        }
        if (user != null) {
            return Response.status(200).entity(user).build();
        } else {
            return Response.status(200).entity(errorMessage).build();
        }
    }

}